#include <system.hh>
#include <sys/types.h>
#include <csignal>
#include <cstring>
#include <netinet/in.h>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <format_interface.hh>
#include <cJSON.h>
#include <string>
#include <fstream>

uint64_t system_load::get_sysup_time()
{
    std::ifstream upTimeFile;
    upTimeFile.open("/proc/uptime");

    if (!upTimeFile.is_open())
    {
        return 0;
    }

    uint64_t beforeBootTime;
    uint64_t sysUptime = 0;
    std::string line;
    while (std::getline(upTimeFile, line))
    {
        sscanf(line.c_str(), "%lu %lu", &sysUptime, &beforeBootTime);
    }
    upTimeFile.close();
    return sysUptime;
}
cJSON *system_load::to_json()
{
    cJSON *cjson = cJSON_CreateObject();
    uint64_t sys_up = get_sysup_time();
    cJSON_AddNumberToObject(cjson, "sysup_time", sys_up);
    return cjson;
}
const char *system_load::get_name()
{
    return "system";
}
